/*
 * @Description:
 * @Version: 2.0
 * @Autor: Seven
 * @Date: 2023-10-16 09:57:29
 * @LastEditors: Seven
 * @LastEditTime: 2023-10-19 13:57:33
 */
import {
  getOperationEquipmentData,
  getOperationEquipmentDetail,
  getOperationEquipmentDelete,
  getOperationEquipmentAdd,
  getOperationDeviceList,
  getOperationDeviceDetail,
  getOperationDeviceRemove,
  getOperationDeviceAdd,
  getOperationOrderFalutList,
  getOperationOrderFalutDetail,
  getOperationOrderFalutRemove,
  getOperationOrderFalutAdd,
} from "@/services";
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";

export const fetchOperationEquip = createAsyncThunk(
  "fetchOperationEquip",
  (payload, { dispatch }) => {
    getOperationEquipmentData().then((res) => {
      dispatch(changeOperationAction(res.rows));
    });
  }
);
export const fetchOperationEquipDetail = createAsyncThunk(
  "fetchOperationEquipDetail",
  (id, { dispatch }) => {
    getOperationEquipmentDetail(id).then((res) => {
      dispatch(changeOperationDetailAction(res.data));
    });
  }
);
export const fetchOperationEquipDelete = createAsyncThunk(
  "fetchOperationEquipDelete",
  (id, { dispatch }) => {
    getOperationEquipmentDelete(id).then((res) => {
      dispatch(changeOperationDeleteAction(res.data));
    });
  }
);
export const fetchOperationEquipAdd = createAsyncThunk(
  "fetchOperationEquipAdd",
  (data, { dispatch }) => {
    getOperationEquipmentAdd(data).then((res) => {});
  }
);
//设备登记
export const fetchOperationDeviceList = createAsyncThunk(
  "fetchOperationDeviceList",
  (data, { dispatch }) => {
    getOperationDeviceList(data).then((res) => {
      dispatch(changeOperationDeviceListAction(res.rows));
    });
  }
);
export const fetchOperationDeviceDetail = createAsyncThunk(
  "fetchOperationDeviceDetail",
  (id, { dispatch }) => {
    getOperationDeviceDetail(id).then((res) => {
      dispatch(changeOperationDeviceDetailAction(res.data));
    });
  }
);

export const fetchOperationDeviceDelete = createAsyncThunk(
  "fetchOperationDeviceDelete",
  (id, { dispatch }) => {
    console.log(id);
    getOperationDeviceRemove(id).then((res) => {
      // dispatch(changeOperationDeviceDeleteAction(res.data));
    });
  }
);

export const fetchOperationDeviceAdd = createAsyncThunk(
  "fetchOperationDeviceAdd",
  (data, { dispatch }) => {
    console.log(data);
    getOperationDeviceAdd(data).then((res) => {
      console.log(res);
      // dispatch(changeOperationDeviceAddAction(res.data));
    });
  }
);

//设备提报
export const fetchOperationOldFaultList = createAsyncThunk(
  "fetchOperationOldFaultList",
  (data, { dispatch }) => {
    getOperationOrderFalutList(data).then((res) => {
      dispatch(changeOperationOldFaultListAction(res.rows));
    });
  }
);
export const fetchOperationOldFaultDetail = createAsyncThunk(
  "fetchOperationOldFaultDetail",
  (data, { dispatch }) => {
    getOperationOrderFalutDetail(data).then((res) => {
      dispatch(changeOperationOldFaultDetaileAction(res.data));
    });
  }
);
export const fetchOperationOldFaultDelete = createAsyncThunk(
  "fetchOperationOldFaultDelete",
  (data, { dispatch }) => {
    getOperationOrderFalutRemove(data).then((res) => {
      // dispatch(changeOperationOldFaultDeleteAction(res.data));
    });
  }
);
export const fetchOperationOldFaultAdd = createAsyncThunk(
  "fetchOperationOldFaultDelete",
  (data, { dispatch }) => {
    getOperationOrderFalutAdd(data).then((res) => {});
  }
);

const operationSlice = createSlice({
  name: "operation",
  initialState: {
    equipList: [],
    equipDetail: {},

    deviceList: [],
    deviceInfo: {},

    OrderFalutList: [],
    OrderFalutInfo: {},
  },
  reducers: {
    changeOperationAction(state, { payload }) {
      state.equipList = payload;
    },
    changeOperationDetailAction(state, { payload }) {
      state.equipDetail = payload;
    },
    changeOperationDeleteAction(state, { payload }) {},
    changeOperationAddAction(state, { payload }) {},

    //设备登记
    changeOperationDeviceListAction(state, { payload }) {
      state.deviceList = payload;
    },
    changeOperationDeviceDetailAction(state, { payload }) {
      state.deviceInfo = payload;
    },
    changeOperationDeviceDeleteAction(state, { payload }) {
      // state.deviceInfo = payload;
    },
    changeOperationDeviceAddAction(state, { payload }) {
      // state.deviceInfo = payload;
    },

    //设备提报
    changeOperationOldFaultListAction(state, { payload }) {
      state.OrderFalutList = payload;
    },
    changeOperationOldFaultAddAction(state, { payload }) {
      state.deviceInfo = payload;
    },
    changeOperationOldFaultRemoveAction(state, { payload }) {
      state.deviceInfo = payload;
    },
    changeOperationOldFaultDeleteAction(state, { payload }) {},
    changeOperationOldFaultDetaileAction(state, { payload }) {
      state.OrderFalutInfo = payload;
    },
    changeOperationOldFaultHandleAction(state, { payload }) {
      state.deviceInfo = payload;
    },
    changeOperationOldFaultGenerateAction(state, { payload }) {
      state.deviceInfo = payload;
    },
  },
});
export default operationSlice.reducer;
export const {
  changeOperationAction,
  changeOperationDetailAction,
  changeOperationDeleteAction,
  changeOperationAddAction,

  changeOperationDeviceListAction,
  changeOperationDeviceDetailAction,
  changeOperationDeviceDeleteAction,
  changeOperationDeviceAddAction,

  //故障提报
  changeOperationOldFaultListAction,
  changeOperationOldFaultAddAction,
  changeOperationOldFaultRemoveAction,
  changeOperationOldFaultDeleteAction,
  changeOperationOldFaultDetaileAction,
  changeOperationOldFaultHandleAction,
  changeOperationOldFaultGenerateAction,
} = operationSlice.actions;
